layui.use(['form','layer','upload','laydate'],function(){
    var form = layui.form
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery;
    var upload = layui.upload;
var laydate=layui.laydate;
    //回显数据
    var request = GetRequest();
    $.get("/admin/travelBusiness/" + request.id,function (travelBusiness) {

        $("#id").val(travelBusiness.id);
        $("#name").val(travelBusiness.name);
        $(".sourcePlace").val(travelBusiness.sourcePlace);

        $(".destination").val(travelBusiness.destination);
        $("#reason").text(travelBusiness.reason);

        // $("#transport").text(travelBusiness.transport);
        (() => {
            let tempArr = Array.prototype.slice.call($('select[name=transport]').children('option'));
            tempArr.splice(0, 1);
            for (let i of tempArr) {
                if (travelBusiness.transport === $(i).text()) {
                    $(i).prop('selected',true);
                    form.render();
                    break;
                }
            }
        })();


        $("#spent").val(travelBusiness.spent);
        $("#remarks").text(travelBusiness.remarks);

        var departTimeDate = new Date(travelBusiness.departTime);
        laydate.render({
            elem: '#departTime'
            ,value: departTimeDate.getFullYear() + '-' + (departTimeDate.getMonth() + 1) + '-' + departTimeDate.getDate() //必须遵循format参数设定的格式  + " " + date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds()
        });
        var date = new Date(travelBusiness.backTime);
        laydate.render({
            elem: '#backTime'
            ,value: date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate() //必须遵循format参数设定的格式  + " " + date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds()
        });

    });

    //修改用户分类信息
    form.on('submit(edit)', function(data){
        var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});

        $.ajax({
            url: '/admin/travelBusiness/edit',
            data: data.field,
            type: 'post',
            success: function (data) {
                if(data.code == 200) {
                    top.layer.close(index);
                    top.layer.msg("修改成功！");
                    layer.closeAll("iframe");
                    //刷新父页面
                    parent.location.reload();
                } else {
                    top.layer.close(index);
                    top.layer.msg("修改失败！");
                }
            },
            error:function () {
                top.layer.close(index);
                top.layer.msg("修改失败！");
            }
        });
        return false;
    });

    //获取路径中的参数
    function GetRequest() {
        var url = location.search;         //获取url中"?"符后的字串
        var theRequest = new Object();
        if (url.indexOf("?") != -1) {
            var str = url.substr(1);
            var strs = str.split("&");
            for (var i = 0; i < strs.length; i++) {
                theRequest[strs[i].split("=")[0]] = decodeURIComponent(strs[i].split("=")[1]);
            }
        }
        return theRequest;
    }

});